package com.cloudshop.service.mongo.controller;

import com.cloudshop.service.common.dto.RestControllerResult;
import com.cloudshop.service.mongo.dto.GetLogByConditionParamsDto;
import com.cloudshop.service.mongo.entity.LogEntity;
import com.cloudshop.service.mongo.service.LogService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 日志Controller.
 *
 * @author 宫新程
 * @since 2019/3/30 0:40
 */
@RestController
@RequestMapping("/log")
public class LogController {

  @Resource private LogService logService;

  /**
   * 查询全部请求日志列表.
   *
   * @return 请求日志列表
   */
  @GetMapping
  public List<LogEntity> findAll() {
    return logService.findAll();
  }

  /** 根据条件查询日志信息. */
  @PostMapping("/get-log-by-condition")
  public RestControllerResult<Page<LogEntity>> getLogByCondition(
      @RequestBody GetLogByConditionParamsDto paramsDto) {
    Page<LogEntity> result = logService.getLogByCondition(paramsDto);
    return RestControllerResult.success(result);
  }

  /**
   * 清空请求日志表.
   *
   * @return Object
   */
  @DeleteMapping
  public Object deleteAll() {
    logService.deleteAll();
    Map<String, Object> result = new HashMap<>();
    result.put("success", true);
    result.put("message", "清除成功");
    return result;
  }
}
